/**
 * 
 */
import java.util.Scanner;
/**
 * @author terratack
 *
 */
public class Poker {

	/**
	 * @param str_Player_Number zmienna przyjmujaca ilosc graczy podana przez uzytkownika w postaci String
	 */
	private static String str_Player_Number;
	/**
	 * @param int_Player_Number zmienna pomocnicza potrzebna do rzutowania String na Integer, zeby moc przeslac ja dalej
	 */
	private static int int_Player_Number;
	/**
	 * @param args
	 */
	public static Player[] player;
	private static Scanner sys_In;
	public static Deck deck;
	public static Table table;
	
	public static void main(String[] args) {
		System.out.println("\t\t\tNOWA GRA W POKERA\n\n\n\t\t     Podaj ilosc graczy(1-4):\n");
		sys_In = new Scanner(System.in);
		str_Player_Number = sys_In.nextLine();
		try{
		int_Player_Number = Integer.parseInt(str_Player_Number);
		}
		catch(NumberFormatException ex){
			System.out.println("podaj liczbe a nie pierdoly...\nodpalam boty\n");
		}
		if(int_Player_Number < 0 || int_Player_Number > 4){ 
			int_Player_Number = 0;
			System.out.println("Podano zla liczbe graczy,\nodpalam same boty\n");
		}
		player = new Player[4];
		int help = 0;
		for(int i = 0; i < 4; i++){
			if(help != int_Player_Number){
				player[i] = new Player("player");
				help++;
			}
			else player[i] = new Player("bot");
		}
		boolean new_part = true;
		boolean licytacja = true;
		while(new_part == true){
			deck = new Deck();
			table = new Table();
			for(int i = 0; i < 4; i++){
				System.out.println("player" + i + "\n");
				for(int j = 0; j < 5; j++){
					System.out.println(player[i].cards_in_hand[j]);
				}
				System.out.println("");
			}			
			while(licytacja == true)
			{
				for(int i = 0;i < 4; i++)
				{
					if(player[i].Pass == false)
					{
						player[i].Licytacja(i,table);
					}
				}
				if(table.postawil[0] == table.postawil[1] && table.postawil[2] == table.postawil[3] && table.postawil[1] == table.postawil[2]){
					licytacja = false;
				}
			}
			licytacja = true;
			System.out.println("\n\n------------------------------------------------\n\n");
			try{
			for(int i = 0; i < int_Player_Number; i++) player[i].human.change_Card(i);
			}
			catch(Exception ex){
				System.out.println("podano zla liczbe");
			}
			for(int i = int_Player_Number; i < 4; i++) player[i].bot.change_Card(i);
			for(int i = 0; i < 4; i++){
				System.out.println("player" + i + "\n");
				for(int j = 0; j < 5; j++){
					System.out.println(player[i].cards_in_hand[j]);
				}
				System.out.println("");
			}
			System.out.println("Druga runda licytacji!");
			while(licytacja == true)
			{
				for(int i = 0;i < 4; i++)
				{
					if(player[i].Pass == false)
					{
						player[i].Licytacja(i,table);
					}
				}
				if(table.postawil[0] == table.postawil[1] && table.postawil[2] == table.postawil[3] && table.postawil[1] == table.postawil[2]){
					licytacja = false;
				}
			}
			licytacja = true;
			int zw = table.jestes_zwyciezca();
			table.najwyzsza_stawka = 0;
			int suma = 0;
			for(int i = 0; i < 4; i++){
				suma += table.postawil[i];
				table.postawil[i] = 0;
			}
			System.out.println("Wygrana: " + suma);
			if(zw == -2) System.out.println("REMIS");
			else{
				player[zw].coins += suma;
				System.out.println("Gracz " + zw + " wygral "+ suma + " aktualny hajs " + player[zw].coins);
			}
			for(int i = 0; i < 4; i++){
				if(i == zw) player[i].wygrane += 1;
				else player[i].przegrane += 1;
				System.out.println("Gracz " + i + " ma aktualnie " + player[i].coins + " hajsiwa");
				System.out.println("Gracz " + i + " ma aktualnie " + player[i].wygrane + " wygranych i " + player[i].przegrane + " przegranych");
			}
			System.out.println("Zaczac nowa gre? Jezeli tak to wpisz tak jezeli nie to nie!");
			String new_game = sys_In.nextLine();
			if(new_game.compareTo("nie") == 0) new_part = false;
			for(int i = 0; i < 4;i++) player[i].Pass = false;
		}
	}
}
